#include "stdafx.h"
#include "decMesh3_pgn_uv_params.h"
#include <math.h>
namespace ds3{

	decMesh3_pgn_uv_params::decMesh3_pgn_uv_params(void)
	{
		Set(0, 0, 1000, 1000, 0);
	}

	decMesh3_pgn_uv_params::~decMesh3_pgn_uv_params(void)
	{
	}

	void decMesh3_pgn_uv_params::Set(float offset_u, float offset_v, float size_u, float size_v, float rotation)
	{
		m_uvParams[0] = offset_u;
		m_uvParams[1] = offset_v;
		//
		double cost = cos(rotation);
		double sint = sin(rotation);
		m_uvParams[2] = offset_u + size_u*cost;
		m_uvParams[3] = offset_v + size_u*sint;
		//
		m_uvParams[4] = offset_u + size_u*cost - size_v*sint;
		m_uvParams[5] = offset_v + size_u*sint + size_v*cost;
		//
		m_uvParams[6] = offset_u - size_v*sint;
		m_uvParams[7] = offset_v + size_v*cost;
	}

	void decMesh3_pgn_uv_params::Set(const GeMatrix2d&tm)
	{
		GePoint2d p0;
		GeVector2d ax, ay;
		tm.getCoordSystem(p0, ax, ay);
		m_uvParams[0] = p0.x;
		m_uvParams[1] = p0.y;
		m_uvParams[2] = p0.x + ax.x;
		m_uvParams[3] = p0.y + ax.y;
		m_uvParams[4] = p0.x + ax.x + ay.x;
		m_uvParams[5] = p0.y + ax.y + ay.y;
		m_uvParams[6] = p0.x + ay.x;
		m_uvParams[7] = p0.y + ay.y;
	}

	void decMesh3_pgn_uv_params::Get(const float*&uvs) const
	{
		uvs = m_uvParams;
	}

	bool decMesh3_pgn_uv_params::Get(GeMatrix2d&tm) const
	{
		GeVector2d ax(m_uvParams[2] - m_uvParams[0], m_uvParams[3] - m_uvParams[1]);
		GeVector2d ay(m_uvParams[6] - m_uvParams[0], m_uvParams[7] - m_uvParams[1]);
		tm.setCoordSystem(GePoint2d(m_uvParams[0], m_uvParams[1]), ax, ay);
		tm.invert();
		return true;
	}
}